<!DOCTYPE html>
<html class="Content">
<head>
    <title>xUnit.js: Data-Driven Tests</title>
    <link type="text/css" rel="Stylesheet" href="../../Style/documentation.css" />
</head>
<body>
    <h1>Data-Driven Tests</h1>
    <p>
        To parameterize console tests, you can use the <strong>Data</strong> attribute to specify the set of values to use when executing the test. 
        These values can be supplied as individual arguments, or as a single Array for the first argument, and will be passed into the test method as an argument.
        This is equivalent to setting <em>Fact</em><strong>.Data</strong>:
        <span class="Code">
[<strong>Data(
    {value:"result",targetClass:StringFactory},
    {value:7357,targetClass:NumberFactory}
)</strong>]
function FactoryStrategiesReturnCorrectTargets(<strong>data</strong>){
    // Arrange
    var expected=<strong>data</strong>.value;
    var target=new <strong>data</strong>.targetClass();

    // Act
    var actual=target.create();
        
    // Assert
    Assert.Equal(expected, actual);
}
        </span>
    </p>
    <p>
        If a data-driven test is also marked asynchronous, the first argument will be the callback method needed to complete the assertion, and the 
        second argument will be the value supplied by the current data iteration:
        <span class="Code">
[Async,<strong>Data(
    {title:"title",url:"http://my.test.site"},
    {title:"other title",url:"http://my.other.test.site"}
)</strong>]
function SetsPageTitle(callback,<strong>data</strong>){
    // Arrange
    var expected=<strong>data</strong>.title;
    var target=<strong>data</strong>.url;

    // Act
    page.load(target, function(pageObject){
        callback(function(){
            var actual=pageObject.title;
        
            // Assert
            Assert.Equal(expected, actual);
        });
    })
}
        </span>
    </p>
    <h3>Notes:</h3>
    <ul>
        <li>
            There is currently a known issue with certain javascript constructs (e.g. shorthand array and object notations) appearing directly in attributes. The workaround
            is to define your complex data in a local variable, and provide that to the Data attribute, e.g.:
            <span class="Code">
<strong>var factoryData=[
    {value:"result",targetClass:StringFactory},
    {value:7357,targetClass:NumberFactory}
];</strong>

[Data(<strong>factoryData</strong>)]
function FactoryStrategiesReturnCorrectTargets(<strong>data</strong>){

    [...]

}
            </span>
        </li>
    </ul>
</body>
</html>